<template>
  <div :xl="6" :lg="7" class="bg-login">
    <!--logo-->
    <el-image :src="require('@/assets/logo.png')" class="logo"/>
    <!--标题-->
    <el-row type="flex" class="row-bg row-two" justify="center" align="middle">
      <el-col :span="6"></el-col>
      <el-col :span="6">
        <!--标题-->
        <h1 class="title">xAdmin管理系统</h1>
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
    <!--form表单-->
    <el-row type="flex" class="row-bg card" justify="center" align="bottom">
      <el-col :span="7" class="login-card">
        <!--loginForm-->
        <el-form style="margin-left: -30px;" :model="loginForm" :rules="rules" ref="loginForm" label-width="21%" class="loginForm">
          <el-form-item label="账户" prop="no" style="width: 380px">
            <el-input v-model="loginForm.no"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 380px">
            <el-input type="password" v-model="loginForm.password" @keyup.enter.native="confirm"></el-input>
          </el-form-item>
          <el-form-item class="btn-ground">
            <el-button type="primary" @click="confirm" :disabled="confirm_disabled">立即登陆</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      // 表单信息
      confirm_disabled: false,
      loginForm: {
        // 账户数据
        no: 'suadmin',
        // 密码数据
        password: '123456'
      },
      // 表单验证
      rules: {
        // 设置账户效验规则
        no: [
          {required: true, message: '请输入账户', trigger: 'blur'}
        ],
        // 设置密码效验规则
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    // 提交表单
     confirm(){
        this.confirm_disabled=true;
        this.$refs.loginForm.validate((valid)=>{
          if (valid){//valid成功为true，失败为false
            //去后台验证用户名和密码
            this.$axios.post(this.$httpUrl+'/user/login',this.loginForm).then(res=>res.data).then(res=>{
              if (res.code===200){
                //存储
                sessionStorage.setItem("CurUser",JSON.stringify(res.data.user))
                this.$store.commit("setMenu",res.data.menu)
                //跳转到主页
                this.$router.replace('/Index');
              }else {
                this.confirm_disabled=false;
                alert('校验失败，用户名或密码错误!');
                return false;
              }
            });
          }else {
            this.confirm_disabled = false;
            return false;
          }
        })
     },
  },
}
</script>

<style scoped>
.codeImg {
  /*让验证码图片飘在右边*/
  float: right;
  /*设置一些圆角边框*/
  border-radius: 3px;
  /*设置宽度*/
  width: 26%;
}

.bg-login {
  height: 100vh;
  background-image: url("../assets/backgroud.jpg");
  background-size: contain;

}

.btn-ground {
  text-align: center;
  margin-left: -21%;
}

.logo {
  margin: 30px;
  height: 70px;
  width: 70px;
  position: fixed;
}

.title {
  text-shadow: -3px 3px 1px #5f565e;
  text-align: center;
  margin-top: 30%;
  color: #41b9a6;
  font-size: 40px;
}

.login-card {
  background-color: #ffffff;
  opacity: 0.9;
  box-shadow: 0 0 20px #ffffff;
  border-radius: 10px;
  padding: 40px 40px 40px 40px;
  width: auto;
}
</style>
